package com.eqxiu.ieditor.api.controller.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eqxiu.ieditor.api.order.IChargeOrderServiceApi;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.interceptor.UserContext;
import com.eqxiu.ieditor.vo.input.OrderInputVO;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
* @description 直冲订单Controller
* @author  
* @modified DuYoulong 重构
* @creatTime 2019年4月12日  
* @since 1.0.0
*/
@RestController
@RequestMapping("/api/m/charge")
public class ChargeOrderApiController {

    private final Logger logger = LoggerFactory.getLogger(ChargeOrderApiController.class);

    @Value("${payment.appid}")
    private String paymentAppId;

//    @Value("${payment.notify}")
//    private String paymentNotify;

    @Value("${payment.notifyCharge}")
    private String paymentNotifyCharge;

    @Value("${payment.appkey}")
    private String appKey;
    
    @Value("${payment.productCode}")
    private String paymentProductCode;

    /**
     * 绑定支付系统的商品id
     */
    private final Integer goodsId = 25;

    @Autowired
    private IChargeOrderServiceApi chargeOrderServiceApi;

    /* 
    // 绑定支付系统的数量
    private final Integer amount = 1;
    // 代表样例模板
    private final int PAYWAY_WECHAT = 0;
    @Autowired
    private OrderService orderService;
    */

    /**
     * @description 创建初始直冲订单<br/>
     *              订单状态未支付
     * @return ResultData
     */
    @ApiOperation(value = "创建初始直冲订单")
    @PostMapping(value = "/create")
    public ResultData createOrder(@ApiParam @RequestBody OrderInputVO vo) {
        com.eqxiu.ieditor.dto.OrderDTO dto = new com.eqxiu.ieditor.dto.OrderDTO();
        BeanUtils.copyProperties(vo, dto);

        dto.setUserId(UserContext.getUser().getId());
        dto.setGoodsId(vo.getGoodsId()!=null?vo.getGoodsId():goodsId);
        dto.setPaymentAppId(paymentAppId);
        dto.setPaymentNotifyCharge(paymentNotifyCharge);
        dto.setPaymentProductCode(paymentProductCode);
        dto.setAppKey(appKey);
        
        return this.chargeOrderServiceApi.createOrder(dto);
    }
}